package com.ideasmaker.action.user;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ideasmaker.bean.User;
import com.ideasmaker.service.user.IUserService;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class UserAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private UserForm userForm;
	
	private IUserService userService;
	
	private HttpServletRequest request ;
	
//	private HttpSession session ;
	
	HttpServletResponse response;


	@Resource
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	public IUserService getUserService() {
		return userService;
	}

	public UserForm getUserForm() {
		return userForm;
	}

	public void setUserForm(UserForm userForm) {
		this.userForm = userForm;
	}

	public String login() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}
	
	public String signUp() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("welcome to idea makers");
		UserForm uf = getUserForm();
		String name = uf.getName();
		User user = convertForm2Bean();
		System.out.println("name: " + name);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.getWriter().print("welcome to idea makers");
		response.getWriter().close();
		userService.saveUser(user);
		return null;
	}
	
	private User convertForm2Bean(){
		if(userForm == null){
			return null;
		}
		
		User user = new User();
		user.setName(userForm.getName());
		user.setPassword(userForm.getPassword());
		user.setGender(userForm.getGender());
		user.setEmail(userForm.getEmail());
		
		return user;
		
	}
	
	public String findUserByName() throws Exception {
		// TODO Auto-generated method stub
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		String name = request.getParameter("name");
		User user = new User();
		user.setName(name);
		String isFind = "true";
		if(!userService.findUserByName(user)){
			isFind = "false";
		}
		
		response.getWriter().print(isFind);
		response.getWriter().close();
		return null;
	}
	
}
